package com.tenement.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tenement.common.GlobalException;
import com.tenement.entity.Menu;
import com.tenement.entity.UserRole;
import com.tenement.enums.RespBusiness;
import com.tenement.mapper.RoleMapper;
import com.tenement.mapper.UserRoleMapper;
import com.tenement.service.MenuService;
import com.tenement.service.RoleMenuService;
import com.tenement.service.UserRoleService;
import com.tenement.service.UserTenementService;
import com.tenement.vo.UserRoleVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.tenement.entity.table.UserRoleTableDef.USER_ROLE;

/**
 *  服务层实现。
 *
 * @author BertXie
 * @since 1.0.1
 */
@Service
@RequiredArgsConstructor
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    private final UserRoleMapper userRoleMapper;

    private final RoleMenuService roleMenuService;

    private final MenuService menuService;

    private final UserTenementService userTenementService;



    @Override
    public boolean operator(UserRoleVo userRoleVo) {
        if (userTenementService.exists(userRoleVo.getUserId()) == 0) {
            throw new GlobalException(RespBusiness.NO_BOUND_PROPERTY);
        }
        userRoleMapper.deleteByQuery(QueryWrapper.create().from(USER_ROLE).where(USER_ROLE.USER_ID.eq(userRoleVo.getUserId())));
        if (userRoleVo.getRoleIds() != null && userRoleVo.getRoleIds().size() > 0) {
            List<UserRole> userRoles = userRoleVo.getRoleIds().stream()
                    .distinct()
                    .map( roleId ->
                            new UserRole().setUserId(userRoleVo.getUserId()).setRoleId(roleId)
                    ).collect(Collectors.toList());
            return userRoleMapper.insertBatch(userRoles) > 0;
        }
        return false;
    }

    @Override
    public List<Long> roleIds(Long userId) {
        return userRoleMapper.selectListByQueryAs(
                QueryWrapper.create()
                        .from(USER_ROLE)
                        .select(USER_ROLE.ROLE_ID)
                        .where(USER_ROLE.USER_ID.eq(userId)), Long.class);
    }

    @Override
    public int remove(List<Long> roleIds) {
        return userRoleMapper.deleteByQuery(QueryWrapper.create().from(USER_ROLE).where(USER_ROLE.ROLE_ID.in(roleIds)));
    }

    @Override
    public List<Menu> roleMenu(Long userId) {
        List<Long> roleIds = this.roleIds(userId);
        if (roleIds != null && roleIds.size() > 0) {
            List<Long> menuIds = roleMenuService.roleMenus(roleIds);
            return menuService.userRoleMenus(menuIds);
        }
        return new ArrayList<>();
    }

    @Override
    public int removeByUserId(List<Long> userId) {
        return userRoleMapper.deleteByQuery(QueryWrapper.create().from(USER_ROLE).where(USER_ROLE.USER_ID.in(userId)));
    }
}
